package m.jclecai.com.adapter.holder.lotteryNumberMenu;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.adapter.base.RyHolder;
import m.jclecai.com.bean.BallItem;
import m.jclecai.com.bean.LotteryNumberMenuBean;
import m.jclecai.com.bean.MissDataBean;
import m.jclecai.com.listener.OnBallCheckedListener;
import m.jclecai.com.view.common.BallView;

/**
 * Created by zed on 2018/3/19.
 */

public class LotteryNumberLineHolder extends RyHolder {
    @BindView(R.id.bv_ball_view)
    BallView ballView;
    @BindView(R.id.tv_missData)
    TextView tv_missData;
    OnBallCheckedListener mOnBallCheckedListener;
    //是否显示遗漏
    boolean isShowMissData = true;

    public LotteryNumberLineHolder(View itemView) {
        super(itemView);
    }

    /**
     * @param bean      菜单栏目
     * @param position  球的数字
     * @param lineIndex 当前哪行的球
     */
    public void init(final int defCheckMenu, final LotteryNumberMenuBean bean, final int position, final int lineIndex) {
        boolean isCheck = false;
        if (bean.getSelectBall() != null) {
            if (bean.getSelectBall().get(lineIndex) != null) {
                if (bean.getSelectBall().get(lineIndex).get(position) != null)
                    isCheck = bean.getSelectBall().get(lineIndex).get(position).isSelected();
            }
        }
        ballView.setChecked(isCheck, false);
        int number = position;
        if (!bean.isStartByZero())
            number += 1;
        ballView.setNumber((number <= 9 ? "0" : "") + String.valueOf(number));
        ballView.setOnCheckedChangeListener(new BallView.OnCheckedChangeListener() {
            @SuppressLint("UseSparseArrays")
            @Override
            public void onCheckedChange(BallView view, boolean isChecked) {
                if (mOnBallCheckedListener != null) {
                    BallItem ballItem = new BallItem();
                    ballItem.setLineIndex(lineIndex);
                    ballItem.setPosition(position);
                    ballItem.setDefCheckMenu(defCheckMenu);
                    ballItem.setBallNum(view.getNumber());
                    ballItem.setSelected(isChecked);
                    mOnBallCheckedListener.onBallChecked(ballItem);
                }
            }
        });
    }

    public void setOnBallCheckedListener(OnBallCheckedListener onBallCheckedListener) {
        mOnBallCheckedListener = onBallCheckedListener;
    }

    public void setMissDataBean(MissDataBean missDataBean, int lineIndex, int position) {
        if (missDataBean == null) return;
        MissDataBean.Omission omission = missDataBean.getOmission();
        if (omission == null) return;
        String mByIndex = omission.getMByIndex(lineIndex);
        if (!TextUtils.isEmpty(mByIndex)) {
            String[] split = mByIndex.split(",");
            if (split.length > position) {
                tv_missData.setText(split[position]);
            } else {
                tv_missData.setText("-");
            }

            if (isShowMissData) {
                tv_missData.setVisibility(View.VISIBLE);
            } else {
                tv_missData.setVisibility(View.GONE);
            }
        }
    }

    public void setShowMissData(boolean isShowMissData) {
        this.isShowMissData = isShowMissData;
    }
}
